package org.amphiprion.rfreplayanalyser.parser;

public class Vector3D {
	public final static Vector3D Z = new Vector3D(0, 0, 1);
	public float x;
	public float y;
	public float z;

	public Vector3D() {
	}

	public Vector3D(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public static Vector3D cross(Vector3D u, Vector3D v) {
		Vector3D uv = new Vector3D();
		uv.x = u.y * v.z - u.z * v.y;
		uv.y = u.z * v.x - u.x * v.z;
		uv.z = u.x * v.y - u.y * v.x;

		return uv;
	}

	public static Vector3D average(Vector3D v1, Vector3D v2) {
		Vector3D moy = new Vector3D();
		moy.x = (v1.x + v2.x) / 2;
		moy.y = (v1.y + v2.y) / 2;
		moy.z = (v1.z + v2.z) / 2;
		return moy;
	}

	public void normalize() {
		float l = (float) Math.sqrt(x * x + y * y + z * z);
		x = x / l;
		y = y / l;
		z = z / l;

	}

	public static Vector3D cross(Vector3D u, Vector3D v, boolean invertX) {
		if (!invertX) {
			return Vector3D.cross(u, v);
		}
		Vector3D uv = new Vector3D();
		uv.x = u.y * v.z - u.z * v.y;
		uv.y = u.z * -v.x - -u.x * v.z;
		uv.z = -u.x * v.y - u.y * -v.x;
		return uv;
	}

	public static Vector3D average(Vector3D v1, Vector3D v2, boolean invertX) {
		if (!invertX) {
			return Vector3D.average(v1, v2);
		}
		Vector3D moy = new Vector3D();
		moy.x = -(v1.x + v2.x) / 2;
		moy.y = (v1.y + v2.y) / 2;
		moy.z = (v1.z + v2.z) / 2;
		return moy;
	}

	@Override
	public String toString() {
		return "" + x + ", " + y + ", " + z;
	}

	public float getLength() {
		return (float) Math.sqrt(x * x + y * y + z * z);
	}
}
